package com.example.demo.web;

import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:lgl
 * @Description:
 * @Date:Create in 2018/1/24 .
 */
@RestController
public class MailController {

    @Autowired
    JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数


    @RequestMapping("mail/send")
    public Object sendEmail() {
        try {
            final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom("939947045@qq.com");
            message.setTo("229093367@qq.com");
           // message.setTo("liguanglun@corefac.com");
            message.setSubject("测试邮件主题");

            StringBuffer sb = new StringBuffer();
            sb.append("<h1>大标题-h1</h1>")
                    .append("<p style='color:#F00'>红色字</p>")
                    .append("<p style='color:#F00'>这里是sprongboot email test</p>")
                    .append("<hr>")
                    .append("<p style='text-align:right'>右对齐</p>");
            message.setText(sb.toString(), true);

          //  message.setText("测试邮件内容");

            //for (int i = 0; i < 10; i++) {
                this.mailSender.send(mimeMessage);
            //}

            return "sucesss";
        } catch (Exception ex) {
            ex.printStackTrace();
            return "error";
        }
    }

    @RequestMapping("mail/send2")
    public void  sendAttachmentsMail(){

        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo("liguanglun@corefac.com");
            helper.setSubject("主题：带附件的邮件");
            helper.setText("带附件的邮件内容");
            //注意项目路径问题，自动补用项目路径
            FileSystemResource file = new FileSystemResource(new File("F:\\Desert.jpg"));
            //加入邮件
            helper.addAttachment("图片.jpg", file);
        } catch (Exception e){
            e.printStackTrace();
        }
            mailSender.send(message);
    }



    @RequestMapping("mail/send3")
    public void sendInlineMail() {
        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(Sender);
            helper.setTo(Sender);
            helper.setSubject("主题：带静态资源的邮件");
            //第二个参数指定发送的是HTML格式,同时cid:是固定的写法
            helper.setText("<html><body>带静态资源的邮件内容 图片:<img src='cid:picture' /></body></html>", true);

            FileSystemResource file = new FileSystemResource(new File("F:\\Desert.jpg"));
            helper.addInline("picture",file);
        } catch (Exception e){
            e.printStackTrace();
        }
        mailSender.send(message);
    }

    @Autowired
    private FreeMarkerConfigurer configurer;

    @RequestMapping("mail/temp")
    public void sendTemplateMail() throws Exception{
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(Sender);
        helper.setTo("liguanglun@corefac.com");
        helper.setSubject("主题");
        Map<String, Object> model = new HashMap<>();
        model.put("id", "006");
        model.put("code", "1578");
       Template template = configurer.getConfiguration().getTemplate("emailTemp.htm");
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);

        helper.setText(text,true);
       mailSender.send(message);
    }
}
